package com.optionexplorer.util;

import java.util.ArrayList;
import java.util.logging.Logger;


/**
 * Utilities class to deal with Strings
 * @author kgajjala
 *
 */

public class StringUtils {

	private static final Logger Log = Logger.getLogger(StringUtils.class.getName());
	
	public StringUtils() {
		
	}	
	
	/**
	 * Checks whether the two string params are equal or not
	 * @param str1
	 * @param str2
	 * @return
	 */
	public static boolean areStringsEqual(String str1, String str2) {
	
		if (str1 == null || str2 == null) {
			return false;
		}
		
		if (str1.equals(str2)) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * Convert a number in String representation to number
	 * e.g., "1,234.56 to 1234.56
	 */	
	public static float strToFloat(String str) {
		float val = 0;
		try {
			str = str.replace(",", "");
			val = Float.parseFloat(str);
		} catch (Exception ex) {
			return 0;
		}
		return val;
	}

	/**
	 * Convert a number in String representation to number
	 * e.g., "1,234.56 to 1234.56
	 */	
	public static long strToLong(String str) {
		long val = 0;
		str = str.replace(",", "");
		val = Long.parseLong(str);				
		return val;
	}	
	
	public static int parseToInt(String str, int defaultValue) {
		int val = 0;
		
		try {
			val = Integer.parseInt(str);			
		} catch (Exception ex) {
			Log.severe("Bad argument for str to Int conversion : " + str);
			val = defaultValue;
		}

		return val;
	}
	
	/**
	 * Method strips the HTML out of the input string and give the pure
	 * contents. It will also strip any white space and compacts the string
	 * @param str
	 * @return
	 */
	public static String stripHTML(String str) {	
		
		StringBuilder sb = new StringBuilder(str);
		int leftIndex = 0;
		int rightIndex = 0;
		String resultStr = null;
		boolean found = false;
		int blankLength = 0;
		
		while (!found){
			leftIndex = sb.indexOf("<");
			if (leftIndex == 0) {
				rightIndex = sb.indexOf(">", leftIndex);
				resultStr = sb.substring(rightIndex+1);
				if (resultStr.equals("")) {
					found = true;
				}
				sb = new StringBuilder(resultStr);
			}
			leftIndex = sb.indexOf("<");
			if (leftIndex > 0) {
				resultStr = sb.substring(0,leftIndex);
				blankLength = resultStr.length();
				resultStr = resultStr.replace(" ", "");				
				if (resultStr.equals("")) {
					sb = new StringBuilder(sb.substring(blankLength));
				} else {
					found = true;
				}
			}
		}		
		return resultStr;
	}
	
	/**
	 * Break a table row into cells
	 */
	
	public static ArrayList<String> rowToColumns(String str) {
		ArrayList<String> colList = new ArrayList<String>();
		
		int pos1, pos2 = 0;
		StringBuilder sb = new StringBuilder(str);
		pos1 = sb.indexOf("<td");
		if (pos1 < 0) {
			System.out.println("Could not find the <td>");
			return null;
		}
		
		String colStr = null;
		while (pos1 > 0) {
			pos2 = sb.indexOf("</td>", pos1);
			colStr = sb.substring(pos1, pos2+5);
			colList.add(stripHTML(colStr));
			pos1 = sb.indexOf("<td", pos2);
		}
		
		return colList;
	}
	
	/**
	 * Purges null value from the input string
	 * @param str
	 * @return
	 */
	public static String purgeNull(String str) {
		if (str == null) {
			return "";
		}
		
		return str;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String str = "hello &amp; hi";
		System.out.println(str.replaceAll("&amp;", "&"));
		
		
	}

}
